import json

import scrapy


class HaokanSpider(scrapy.Spider):
    name = "haokan"

    # allowed_domains = ["aaa.com"]
    # start_urls = ["https://aaa.com"]
    async def start(self):
        yield scrapy.Request("https://haokan.baidu.com/")

    def parse(self, response):
        datas = response.xpath('//*[@id="__NEXT_DATA__"]/text()').get()
        items = json.loads(datas)
        for item in items["props"]["pageProps"]["pageSSRData"]["top_video_list"]:
            print(item)
            title = item["title"]
            auther = item["source_name"]
            img_url = item["poster_pc"]
            plat_url = item["play_url"]
            link_url = item["url"]
            data = {
                "title": title,
                "auther": auther,
                "img_url": img_url,
                "plat_url": plat_url,
                "link_url": link_url,
            }
            yield data
